package com.jzl.swing.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import com.jzl.swing.entity.User;
import com.jzl.swing.service.UserService;
import com.jzl.swing.service.impl.UserServiceImpl;

import javax.swing.JPasswordField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddUserUI {

	private JFrame frame;
	private JTextField textField;
	private JPasswordField passwordField;
	private JPasswordField passwordField_1;
	UserService userService = new UserServiceImpl();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddUserUI window = new AddUserUI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public AddUserUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 494);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("用户名称：");
		label.setBounds(83, 34, 76, 16);
		frame.getContentPane().add(label);
		
		textField = new JTextField();
		textField.setBounds(180, 29, 130, 26);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel label_1 = new JLabel("用户密码：");
		label_1.setBounds(83, 91, 76, 16);
		frame.getContentPane().add(label_1);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(180, 86, 123, 26);
		frame.getContentPane().add(passwordField);
		
		JLabel label_2 = new JLabel("确认密码：");
		label_2.setBounds(83, 149, 76, 16);
		frame.getContentPane().add(label_2);
		
		passwordField_1 = new JPasswordField();
		passwordField_1.setBounds(180, 144, 123, 26);
		frame.getContentPane().add(passwordField_1);
		
		JLabel label_3 = new JLabel("用户权限：");
		label_3.setBounds(83, 231, 76, 16);
		frame.getContentPane().add(label_3);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"普通员工", "管理员"}));
		comboBox.setBounds(180, 227, 76, 27);
		frame.getContentPane().add(comboBox);
		
		JButton button = new JButton("添加");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String user_name = textField.getText();
				String user_pass = String.valueOf(passwordField.getPassword());
				String user_repa = String.valueOf(passwordField_1.getPassword());
				String user_iden = comboBox.getSelectedItem().toString();
				if (!user_pass.equals(user_repa)) {
					JOptionPane.showMessageDialog(frame, "两次密码输入不一致");
					return;
				}
				try {
					User user= new User();
					user.setUser_name(user_name);
					user.setUser_pass(user_pass);
					user.setUser_iden(user_iden);
					if (userService.insertUser(user) != 1) {
						JOptionPane.showMessageDialog(frame, "插入用户失败");
					}else {
						JOptionPane.showMessageDialog(frame, "插入成功");
						frame.dispose();
					}
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
				
			}
		});
		button.setBounds(71, 342, 117, 29);
		frame.getContentPane().add(button);
		
		JButton button_1 = new JButton("关闭");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int i = JOptionPane.showConfirmDialog(frame, "您输入的数据将不会被报错");
				if (i==0) {
					frame.dispose();					
				}
			}
		});
		button_1.setBounds(240, 342, 117, 29);
		frame.getContentPane().add(button_1);
	}
}
